// Copyright 2019-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// mqbmock_logidgenerator.cpp                                         -*-C++-*-
#include <mqbmock_logidgenerator.h>

#include <mqbscm_version.h>
// MQB
#include <mqbs_storageutil.h>

// BDE
#include <bsls_assert.h>

namespace BloombergLP {
namespace mqbmock {

// --------------------
// class LogIdGenerator
// --------------------

// CREATORS
LogIdGenerator::LogIdGenerator(const char* prefix, bslma::Allocator* allocator)
: d_logPrefix(prefix, allocator)
, d_logIds(allocator)
, d_nextId(0)
{
    // NOTHING
}

LogIdGenerator::~LogIdGenerator()
{
    // NOTHING
}

bool LogIdGenerator::registerLogId(const mqbu::StorageKey& logId)
{
    return d_logIds.emplace(logId).second;
}

void LogIdGenerator::generateLogId(bsl::string*      logName,
                                   mqbu::StorageKey* logId)
{
    BSLS_ASSERT_OPT(logName);
    BSLS_ASSERT_OPT(logId);

    *logName = d_logPrefix + bsl::to_string(d_nextId++) + ".bmq";
    mqbs::StorageUtil::generateStorageKey(logId, &d_logIds, *logName);
}

}  // close package namespace
}  // close enterprise namespace
